package lolic;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
 * 
 * @author Dave DePiano
 *
 */
public class ChampCollector extends ArrayList<Champ>
{
	public ChampCollector (JProgressBar progressBar, Document docChamps)
	{
		getChamps(progressBar, docChamps);
		alphabetize();
	}	
	
	public ImageIcon getImage(String strName, int intID)
	{
		Image img;
		//first, search for image locally
		File f = new File("champs/" + intID + ".jpg");
		if(f.exists())
		{
			ImageIcon imgIcon = new ImageIcon("champs/" + intID + ".jpg");
			return imgIcon;
		}		
		else //if not found local, search leagueoflegends.com site
		{

			try {
				URL url = new URL(Statics.strLolChampPortraitsURL + intID
						+ ".jpg");
				img = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit()
				.createImage(url);
				ImageIcon imgIcon = new ImageIcon(img);
				
				if (imgIcon.getIconWidth() < 1) //image is not on the site either, use a blank image 
				{
					imgIcon = new ImageIcon("blank.png");
					System.err.println("--" + Statics.strLolChampPortraitsURL + intID
					+ ".jpg not found");
				}
				else
				{
					//save image from site to local file
					BufferedImage bi = new BufferedImage(imgIcon.getIconWidth(),imgIcon.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
					Graphics2D g2 = bi.createGraphics();
					//Draw img into bi so we can write it to file.
					g2.drawImage(img, 0, 0, null);
					g2.dispose();
					ImageIO.write(bi, "jpg", new File("champs/" + intID + ".jpg"));
				}
				
				return imgIcon;
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

	private void getChamps(JProgressBar progressBar, Document docChamps) 
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
			
		docChamps.getDocumentElement ().normalize ();        
		NodeList listOfChamps =  docChamps.getElementsByTagName("Champ");
		
		int i = 0;		
		
		while(listOfChamps.item(i) != null)
		{
			Element eleChamp = (Element)listOfChamps.item(i);
			int intID = new Integer(eleChamp.getAttribute("id"));
			String strName = eleChamp.getAttribute("name");
			Champ champ = new Champ(strName, intID);
			ImageIcon imgIcon = getImage(strName, intID);
			champ.setImgPortrait(imgIcon);
			add(champ);
			//itemNames.add(strName);
			i++;
			progressBar.setValue(i);
		}
	}
	
	private void alphabetize() 
	{		
		Collections.sort(this, new Comparator<Object>(){
			 
            public int compare(final Object o1, final Object o2) {
                final Champ i1 = (Champ) o1;
                final Champ i2 = (Champ) o2;
               return i1.toString().compareToIgnoreCase(i2.toString());
            } 
        });
	}
}

